package ModelLayer;
import java.util.ArrayList;

/**
 * A class to represent an order. Holds a customer and a list of partorders
 * 
 * @author Mike M. Madsen, Rasmus A. Borup
 * @version 15/12-2013
 */
public class Order
{
    // instance variables - replace the example below with your own
    private static String orderNO = "000000";
    private String orderID;
    private String customerType;
    private ArrayList<PartOrder> partOrders;
    private Customer customer;
    private boolean wantedDelivery;
    private double totalPrice;
    private String lastPayDate;
    private String payStatus;

    /**
     * Constructor for objects of class Order without parameters for easy testing
     */
    public Order()
    {
        nextID();
        partOrders = new ArrayList<PartOrder>();
    }
    
    /**
     * Constructor for objects of class Order
     */
    public Order(String customerType, Customer customer, boolean wantedDelivery)
    {
        nextID();
        this.customerType = customerType;
        partOrders = new ArrayList<PartOrder>();
        this.customer = customer;
        this.wantedDelivery = wantedDelivery;
        totalPrice = calculateTotalPrice();
        lastPayDate = getLastPayDate();
        payStatus = "Pending";
        partOrders = new ArrayList<PartOrder>();
    }
    
    /**
     * Counts the orderID string up one by parsing it to int, adding one, parsing it back to String
     * and using zeroString() to add the zeroes to the start of the string
     */
    private void nextID()
    {
        int intID = Integer.parseInt(orderNO);
        intID++;
        orderNO = zeroString() + Integer.toString(intID);
        orderID = orderNO;
    }
    
    /**
     * Returns number of zeroes in the start of the orderID String
     */
    private int countZeroes()
    {
        int i = 0;
        boolean done = false;
        int noOfZeroes = 0;
        while(!done && i < 5)
        {
            if(orderNO.charAt(i) == '0')
            {
                noOfZeroes++;
                i++;
            }
            else
            {
                done = true;
            }
        }
        return noOfZeroes;
    }
    
    /**
     * Returns a string containing the amount of zeroes in the start of the id String using countZeroes()
     */
    private String zeroString()
    {
        String zeroString = "";
        for(int i = 0; i < countZeroes(); i++)
        {
            zeroString += "0";
        }
        return zeroString;
    }
    
    /**
     * Returns a string containing details about an order
     */
    public String getDetailsString()
    {
        String details = "\fOrder details:";
        details += "\nOrder ID: " + getOrderID() + "\n" + "Customer type: " + getCustomerType() + "\n";
        details += "Delivery wanted?: " + getWantedDelivery() + "\n";
        details += "Last payment day: " + getLastPayDate() + "\n" + "Payment status: " + getPayStatus() + "\n\n";
        if(customer != null)
        {
            details += getCustomer().detailsString();
        }
        details += "Products: \n";
        if (partOrders.size() != 0)
        {
            for (int i = 0; i < partOrders.size(); i++)
            {
                details += partOrders.get(i).getPartOrderDetails();
                calculateTotalPrice();
            }
            
        }
        else if (partOrders.size() == 0 || partOrders == null)
        {
            details += "None!";
        }
        System.out.println(details);
        return details;
    }  
    
    /**
     * Adds a partorder to the orders partorder list
     */
    public void addPartOrder(PartOrder partOrder)
    {
        partOrders.add(partOrder);
    }
    
    /**
     * Calculates the total price of the order based on the partorders
     */
    public double calculateTotalPrice()
    {
        totalPrice = 0;
        for (PartOrder partOrder : partOrders)
        {
            totalPrice = totalPrice + partOrder.calculateTotalPartOrderPrice();
        }
        return totalPrice;
    }
    
    //Mutators and accessors
    public String getLastPayDate()
    { 
        return lastPayDate;
    }
    
    public String getOrderID()
    {
        return orderID;
    }
    
    public String getCustomerType()
    {
        return customerType;
    }
    
    public void setCustomerType(String customerType)
    {
        this.customerType = customerType;
    }
    
    public Customer getCustomer()
    {
        return customer;
    }
    
    public void setCustomer(Customer customer)
    {
        this.customer = customer;
    }
    
    public boolean getWantedDelivery()
    {
        return wantedDelivery;
    }
    
    public void setWantedDelivery(boolean wantedDelivery)
    {
        this.wantedDelivery = wantedDelivery;
    }
    
    public double getPrice()
    {
        return totalPrice;
    }
    
    public String getPayStatus()
    {
        return payStatus;
    }
    
    public void setPayStatus(String payStatus)
    {
        this.payStatus = payStatus;
    }
}
    